#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileDialog>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    mplayer = new ijkplayerImp();

    connect(ui->Button_open,SIGNAL(clicked()),this,SLOT(slotBtnClick()));
    connect(ui->Button_start,SIGNAL(clicked()),this,SLOT(slotBtnClick()));
    connect(ui->Button_pause,SIGNAL(clicked()),this,SLOT(slotBtnClick()));
    connect(ui->Button_stop,SIGNAL(clicked()),this,SLOT(slotBtnClick()));

    connect(ui->horizontalSlider,SIGNAL(sliderMoved(int)),this,SLOT(slotSliderMoved(int)));

    connect(mplayer,SIGNAL(sig_prepared(qint64)),this,SLOT(slotPrepared(qint64)));
}

MainWindow::~MainWindow()
{
    mplayer->stopplay();
    delete mplayer;
    mplayer = nullptr;
    delete ui;
}

void MainWindow::slotPrepared(qint64 msec)
{
    qint64 Sec = msec/1000;

    ui->horizontalSlider->setRange(0,Sec);

//    QString hStr = QString("00%1").arg(Sec/3600);
    QString mStr = QString("00%1").arg(Sec/60);
    QString sStr = QString("00%1").arg(Sec%60);

    QString str = QString("%1:%2").arg(mStr.right(2)).arg(sStr.right(2));
    ui->label_totaltime->setText(str);
}

void MainWindow::slotSliderMoved(int value)
{
    if (QObject::sender() == ui->horizontalSlider)
    {
        mplayer->seekto((long)value * 1000);
    }
}


void MainWindow::slotBtnClick()
{
    if (QObject::sender() == ui->Button_start)
    {
        mplayer->startplay();
    }
    else if (QObject::sender() == ui->Button_pause)
    {
        mplayer->pause();
    }
    else if (QObject::sender() == ui->Button_stop)
    {
        mplayer->stopplay();
    }
    else if (QObject::sender() == ui->Button_open)
    {
        QString s = QFileDialog::getOpenFileName(
                   this, "选择要播放的文件",
                    "/",//初始目录
                    "视频文件 (*.flv *.rmvb *.avi *.MP4);; 所有文件 (*.*);; ");
        if (!s.isEmpty())
        {
            //s.replace("/","\\");
            mplayer->stopplay(); //如果在播放则先停止
            mplayer->setdatasource(s.toUtf8().data());
        }
    }
}

